require 'socket'

module ConfigHuExtension
  def self.included(base)
    base.send(:alias_method, :load_environment_without_hu_config, :load_environment)
    base.send(:alias_method, :load_environment, :load_environment_with_hu_config)
  end

  def load_environment_with_hu_config
    load_environment_without_hu_config
    load_hu_file "hosts/#{Socket.gethostname.split('.').first}.rb"
    load_hu_file "users/#{ENV['USER']}.rb"
  end

  private
  def load_hu_file(file)
    config = configuration
    file = File.join(RAILS_ROOT, 'config', file)
    if File.exists?(file)
      constants = self.class.constants
      eval(IO.read(file), binding, file)
      (self.class.constants - constants).each do |const|
        Object.const_set(const, self.class.const_get(const))
      end
    end
  end
end

Rails::Initializer.send(:include, ConfigHuExtension)
